package com.jtny.nytb.dataparam.service.serviceImpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jtny.component.util.Assert;
import com.jtny.nytb.config.dataalarm.dao.DataAlarmDAO;
import com.jtny.nytb.config.dataalarm.domain.DataAlarmDO;
import com.jtny.nytb.dataparam.dao.ConfigDataParamAlgorithmParamRelationDAO;
import com.jtny.nytb.dataparam.dao.ConfigDataParamAlgorithmRelationDAO;
import com.jtny.nytb.dataparam.domain.*;
import com.jtny.nytb.dataparam.vo.ConfigAlarmVO;
import com.jtny.nytb.dataparam.vo.ConfigDataParamManagementVO;
import com.jtny.nytb.dataparam.vo.ConfigParamVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class DataParamAsyncServiceImpl {

    private ConfigDataParamAlgorithmRelationDAO algorithmRelationDAO;
    private DataAlarmDAO dataAlarmDAO;
    private ConfigDataParamAlgorithmParamRelationDAO paramRelationDAO;
    /**
     * 更新算法和参数依赖。
     *
     * @param configDataParamManagementVO
     * @param manageId
     */
//    @Async
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateAlgorithm(ConfigDataParamManagementVO configDataParamManagementVO, Long manageId) {
        // 删除上次的关联关系
        algorithmRelationDAO.delete(Wrappers.<ConfigDataParamAlgorithmRelationDO>lambdaQuery()
                .eq(ConfigDataParamAlgorithmRelationDO::getDataParamId, manageId));
        // 插入数据变量算法依赖
        if (configDataParamManagementVO.getAlgorithmId() != null) {
            ConfigDataParamAlgorithmRelationDO configDataParamAlgorithmRelationDO = new ConfigDataParamAlgorithmRelationDO();
            configDataParamAlgorithmRelationDO.setDataParamId(manageId);
            configDataParamAlgorithmRelationDO.setAlgorithmId(configDataParamManagementVO.getAlgorithmId());
            algorithmRelationDAO.insert(configDataParamAlgorithmRelationDO);
        }
        // 更新参数参量对应数据
        saveOrUpdateParamList(configDataParamManagementVO);
    }

    /**
     * 保存报警数据
     *
     * @param configDataParamManagementVO
     * @param configDataParamManagementDO
     */
//    @Async
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateAlarmList(ConfigDataParamManagementVO configDataParamManagementVO,
                                      ConfigDataParamManagementDO configDataParamManagementDO) {
        dataAlarmDAO.delete(Wrappers.<DataAlarmDO>lambdaQuery().eq(DataAlarmDO::getDataParamId, configDataParamManagementVO.getId()));

        List<ConfigAlarmVO> configAlarmVOList= configDataParamManagementVO.getConfigAlarmVOList();
        if (CollectionUtil.isEmpty(configAlarmVOList)) {
            return;
        }
        // 保存报警数据到报警表中
        List<DataAlarmDO> dataAlarmIdList = new ArrayList<>();
        configAlarmVOList.forEach(configAlarmVO -> {
            DataAlarmDO dataAlarmDO = new DataAlarmDO();
            dataAlarmDO.setCode(configDataParamManagementVO.getCode());
            dataAlarmDO.setName(configDataParamManagementDO.getName());
            dataAlarmDO.setSource(configDataParamManagementVO.getSource());
            dataAlarmDO.setAlarmClass(configAlarmVO.getAlarmClass());
            dataAlarmDO.setAlarmDescribe(configAlarmVO.getAlarmDescribe());
            dataAlarmDO.setAlarmCondition(configAlarmVO.getAlarmCondition());
            dataAlarmDO.setAlarmName(configAlarmVO.getAlarmName());
            dataAlarmDO.setAlarmType(configAlarmVO.getAlarmType());
            dataAlarmDO.setDataParamId(configDataParamManagementDO.getId());
            dataAlarmIdList.add(dataAlarmDO);
        });
        // 批量保存报警数据到报警表中
        dataAlarmDAO.insertBatch(dataAlarmIdList);
    }

    /**
     * 更新参数参量对应数据。
     */
    private void saveOrUpdateParamList(ConfigDataParamManagementVO configDataParamManagementVO) {
        Long id = configDataParamManagementVO.getId();
        Assert.notNull(id, "参量id不能为空");

        paramRelationDAO.delete(Wrappers.<ConfigDataParamAlgorithmParamRelationDO>lambdaQuery()
                .eq(ConfigDataParamAlgorithmParamRelationDO::getParentParamId, id));
//        deleteLastParamRelation(configDataParamAlgorithmRelationDO, configDataParamManagementVO.getId());
        // 判定当前有没有需要插入的参数数据，如果没有则返回。
        List<ConfigParamVO> configParamVOList = configDataParamManagementVO.getConfigParamVOList();
        if (CollUtil.isEmpty(configParamVOList)) {
            return;
        }
        List<ConfigDataParamAlgorithmParamRelationDO> dataAlgorithParamRelationDOList = new ArrayList<>();
        configParamVOList.forEach(configParamVO -> {
            // 剔除 全局变量
            if ("全局变量".equals(configParamVO.getParamType())) {
                return;
            }
            ConfigDataParamAlgorithmParamRelationDO configDataParamAlgorithmParamRelationDO = new ConfigDataParamAlgorithmParamRelationDO();
            configDataParamAlgorithmParamRelationDO.setAlgorithParamId(configParamVO.getParamId());
            configDataParamAlgorithmParamRelationDO.setDataParamId(configParamVO.getVariableId());
            configDataParamAlgorithmParamRelationDO.setParentParamId(id);
            dataAlgorithParamRelationDOList.add(configDataParamAlgorithmParamRelationDO);
        });
        if (CollUtil.isEmpty(dataAlgorithParamRelationDOList)) {
            return;
        }
        paramRelationDAO.insertBatch(dataAlgorithParamRelationDOList);
    }

}

